package moni.anyou.com.view.view.account;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import org.json.JSONArray;
import org.json.JSONObject;
import org.kymjs.aframe.http.KJHttp;
import org.kymjs.aframe.http.KJStringParams;
import org.kymjs.aframe.http.StringCallBack;

import java.util.ArrayList;

import moni.anyou.com.view.R;
import moni.anyou.com.view.base.BaseActivity;
import moni.anyou.com.view.config.SysConfig;
import moni.anyou.com.view.service.UpFileService;
import moni.anyou.com.view.tool.AppTools;
import moni.anyou.com.view.tool.ToastTools;
import moni.anyou.com.view.tool.Tools;
import moni.anyou.com.view.tool.VerificationTools;
import moni.anyou.com.view.view.IndexActivity;
import moni.anyou.com.view.view.account.adapter.SelectKinderGartenAdapter;
import moni.anyou.com.view.widget.NetProgressWindowDialog;

public class SelectKindergartenActivity extends BaseActivity implements View.OnClickListener {

    private NetProgressWindowDialog window;
    private SelectKinderGartenAdapter adapter;
    private ListView  mListView;
    JSONArray list = new JSONArray();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_select_garden);
        init();

    }


    @Override
    public void initView() {
        super.initView();
        initTitle();
        ivBack.setVisibility(View.GONE);
        tvTitle.setText("切换幼儿园");
        window = new NetProgressWindowDialog(mBaseActivity);
        mListView = (ListView) findViewById(R.id.lv_select);
        mListView.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {
                try {
                    SysConfig.cid = ((JSONObject) list.get(position)).get("companyID").toString();
                    SysConfig.cname =  ((JSONObject) list.get(position)).get("company").toString();
                    startActivity(new Intent(SelectKindergartenActivity.this, IndexActivity.class));
                }
                catch(Exception ex){

                }

                finish();

            }
        });

        //adapter =  new SelectKinderGartenAdapter(this,list);
        //mListView.setAdapter(adapter);
        getPost();
    }




    @Override
    public void onClick(View view) {
        switch (view.getId()) {
                default:
                break;
        }
    }



    public void getPost() {

        KJHttp kjh = new KJHttp();
        kjh.setTimeout(60000);
        KJStringParams params = new KJStringParams();
        String cmdPara = "{\"cmd\":\"12\",\"uid\":\"" + SysConfig.uid + "\",\"token\":\"" + SysConfig.token + "\"}";
        params.put("sendMsg", cmdPara);
        window.ShowWindow();
        kjh.urlGet(SysConfig.ServerUrl, params, new StringCallBack() {
            @Override
            public void onSuccess(String t) {


                try {
                    JSONObject jsonObject = new JSONObject(t);

                    int result = Integer.parseInt(jsonObject.getString("result"));
                    if (result >= 1) {
                        list = jsonObject.getJSONArray("list");
                       // Toast.makeText(mContext, ""+ list.length(), Toast.LENGTH_LONG).show();
                        //adapter.notifyDataSetChanged();
                        adapter =  new SelectKinderGartenAdapter(SelectKindergartenActivity.this,list);
                        mListView.setAdapter(adapter);

                    }else {
                        if (result == -2){
                            AppTools.jumptoLogin(SelectKindergartenActivity.this);
                        }
                        Toast.makeText(mContext, jsonObject.get("retmsg").toString(), Toast.LENGTH_LONG).show();
                    }
                } catch (Exception ex) {
                    Toast.makeText(mContext, "数据请求失败", Toast.LENGTH_LONG).show();

                }
                window.closeWindow();
            }




            @Override
            public void onFailure(Throwable t, int errorNo, String strMsg) {
                Toast.makeText(mContext, "网络异常，请稍后再试", Toast.LENGTH_LONG).show();

                window.closeWindow();
            }
        });
    }

}
